<template>
  <div class="switch-option">
    <span class="name">{{name}}</span>
    <div class="switch-ellipse" :class="{'active': checked}" @click="clickSwitch">
      <div class="switch-circle" :class="{'active': checked}"></div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'SwitchOption',
  props: {
    name: {
      type: String,
      default: ''
    },
    value: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      checked: this.value
    }
  },
  watch: {
    checked (newVal) {
      this.$emit('update:value', newVal)
    }
  },
  methods: {
    clickSwitch (newVal) {
      this.checked = !this.checked
    }
  }
}
</script>

<style lang="scss">
.switch-option {
  display: flex;
  justify-content: space-between;
  width: 100%;
  height: 100%;
  padding: 15px 15px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  .name {
    align-self: center;
    font-size: 16px;
  }
  .switch-ellipse {
    align-self: center;
    position: relative;
    width: 49px;
    height: 26px;
    overflow: hidden;
    background: rgba(128, 128, 128, 0.5);
    border-radius: 15px;
    cursor: pointer;
    transition: all 0.1s ease;
    &.active {
      background: #7eae4b;
    }
    .switch-circle {
      position: absolute;
      left: 0;
      width: 26px;
      height: 26px;
      background: white;
      border-radius: 50%;
      box-shadow: 0px 0px 1px rgba(128, 128, 128, .3) inset;
      transition: all 0.2s ease 0.1s;
      &.active {
        left: 23px;
      }
    }
  }
}
</style>
